﻿/***************************************************************
*       
* add by OceanHo 2015/8/25 15:24:38
*
****************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace oceanho.webapi.factory.httpUtility
{
    /// <summary>
    /// 定义一个工具类。用于管理委托函数列表
    /// </summary>
    internal sealed class DelegateEventUtility
    {
        /// <summary>
        /// 获取对象上，某个事件的委托调用列表（注:此方法目前仅仅适用于 UploadParameter 对象）
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="objT">对象</param>
        /// <param name="eventName">事件字段名称</param>
        /// <returns>委托调用列表</returns>
        public static Delegate[] GetObjectEventList<T>(T objT, string eventName)
        {
            FieldInfo _filedInfo = objT.GetType().GetField("Events",
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            if (_filedInfo == null) return null;
            object _EventList = _filedInfo.GetValue(objT);
            EventHandlerList _list = (EventHandlerList)_EventList;
            if (_list == null) return null;

            Delegate _ObjectDelegate = _list[eventName];
            if (_ObjectDelegate == null) return null;
            return _ObjectDelegate.GetInvocationList();
        }
    }
}
